package cn.tqfeiyang.smartadmin.business.organization.controller;

import cn.tqfeiyang.smartadmin.business.organization.constant.DepartmentConstants;
import cn.tqfeiyang.smartadmin.business.organization.service.DepartmentCurdService;
import cn.tqfeiyang.smartadmin.business.organization.request.DepartmentCreateDTO;
import cn.tqfeiyang.smartadmin.business.organization.request.DepartmentUpdateDTO;
import cn.tqfeiyang.smartadmin.business.organization.request.DepartmentQueryDTO;
import cn.tqfeiyang.smartadmin.business.organization.response.DepartmentQueryVO;
import cn.tqfeiyang.smartadmin.business.organization.response.DepartmentDetailVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;

import java.util.List;

/**
 * 部门CURD控制器
 *
 * @author tqfeiyang
 * @since 2024-10-18
 */
@RestController
@Validated
@RequestMapping(DepartmentConstants.REQUEST_URL)
@Tag(name = DepartmentConstants.API_TAG)
public class DepartmentCurdController {

    /**
     * 部门CURD服务
     */
    @Resource
    private DepartmentCurdService departmentCurdService;

    /**
     * 分页查询部门
     */
    @OperationLog
    @GetMapping("/queryDepartments")
    @Operation(summary = "部门 - 分页查询部门")
    @SaCheckPermission(DepartmentConstants.PERMISSION + "queryDepartments")
    public ApiResult<PageData<DepartmentQueryVO>> queryDepartments(@Validated DepartmentQueryDTO dto) {
        return ApiResult.success(departmentCurdService.queryDepartments(dto));
    }

    /**
     * 查询部门详情
     */
    @OperationLog
    @GetMapping("/readDepartment/{departmentId}")
    @Operation(summary = "部门 - 查询部门详情")
    @SaCheckPermission(DepartmentConstants.PERMISSION + "readDepartment")
    public ApiResult<DepartmentDetailVO> readDepartment(@PathVariable @EntityId Long departmentId) {
        return ApiResult.success(departmentCurdService.readDepartment(departmentId));
    }

    /**
     * 添加部门
     */
    @OperationLog
    @PostMapping("/createDepartment")
    @Operation(summary = "部门 - 添加部门")
    @SaCheckPermission(DepartmentConstants.PERMISSION + "createDepartment")
    public ApiResult<DepartmentDetailVO> createDepartment(@RequestBody @Validated DepartmentCreateDTO dto) {
        Long departmentId = departmentCurdService.createDepartment(dto);
        return ApiResult.success(departmentCurdService.readDepartment(departmentId));
    }

    /**
     * 修改部门
     */
    @OperationLog
    @PostMapping("/updateDepartment/{departmentId}")
    @Operation(summary = "部门 - 修改部门")
    @SaCheckPermission(DepartmentConstants.PERMISSION + "updateDepartment")
    public ApiResult<DepartmentDetailVO> updateDepartment(@PathVariable @EntityId Long departmentId, @RequestBody @Validated DepartmentUpdateDTO dto) {
        departmentCurdService.updateDepartment(departmentId, dto);
        return ApiResult.success(departmentCurdService.readDepartment(departmentId));
    }

    /**
     * 删除部门
     */
    @OperationLog
    @PostMapping("/deleteDepartment/{departmentId}")
    @Operation(summary = "部门 - 删除部门")
    @SaCheckPermission(DepartmentConstants.PERMISSION + "deleteDepartment")
    public ApiResult deleteDepartment(@PathVariable @EntityId Long departmentId) {
        departmentCurdService.deleteDepartment(departmentId);
        return ApiResult.success();
    }

    /**
     * 批量删除部门
     */
    @OperationLog
    @PostMapping("/batchDeleteDepartments")
    @Operation(summary = "部门 - 批量删除部门")
    @SaCheckPermission(DepartmentConstants.PERMISSION + "batchDeleteDepartments")
    public ApiResult batchDeleteDepartments(@RequestBody List<Long> departmentIds) {
        departmentCurdService.batchDeleteDepartments(departmentIds);
        return ApiResult.success();
    }
}
